package com.xydzxt.www.controller.student;

import com.xydzxt.www.model.Examinee;
import com.xydzxt.www.model.Major;
import com.xydzxt.www.model.Regist;
import com.xydzxt.www.service.ExamineeService;
import com.xydzxt.www.service.MajorService;
import com.xydzxt.www.service.RegistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ScoreQueryController {
    @Autowired
    RegistService registService;
    @Autowired
    ExamineeService examineeService;
    @Autowired
    MajorService majorService;

    @GetMapping("/scoreQuery")
    public String scoreQuery(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session=req.getSession();

        if (session.getAttribute("sfzh")!=null){
            String idNumber=session.getAttribute("sfzh").toString();
            Regist regist=registService.selectByIdNumber(idNumber);
            Examinee examinee=examineeService.selectByRegistId(regist.getRegistId());

            if (examinee!=null){
                req.setAttribute("regist",regist);
                req.setAttribute("examinee",examinee);
                List<Integer> majorIdList=new ArrayList<>();
                List<Major> majorList=new ArrayList<>();
                majorIdList.add(regist.getFirstChoice());
                majorIdList.add(regist.getSecondChoice());
                majorIdList.add(regist.getThirdChoice());

                for (int i=0;i<majorIdList.size();i++){
                    majorList.add(majorService.selectById(majorIdList.get(i)));
                }
                req.setAttribute("majorList",majorList);
            }else {
                req.setAttribute("msg","请先进行报考缴费！完成考试后3个工作日内可查询成绩！");
                return "student/msg";
            }
            return "student/scoreQuery";

        }else {
            resp.sendRedirect("index");
        }
        return "";
    }
}
